package com.xuecheng.content.listener;

import com.xuecheng.base.constants.RabbitMqConstants;
import com.xuecheng.content.feignclient.CourseIndex;
import com.xuecheng.content.model.po.CoursePublish;
import com.xuecheng.content.service.CoursePublishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author itcast
 * @create 2024-09-25 11:34
 */
@Slf4j
@Component
public class CoursePublishListener {
    @Resource
    private CoursePublishService coursePublishService;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = RabbitMqConstants.Queue.COURSE_PUBLISH),
            exchange = @Exchange(name = RabbitMqConstants.Exchange.CONTENT, type = ExchangeTypes.TOPIC),
            key = RabbitMqConstants.Key.COURSE_PUBLISH))
    public void listenCoursePublishMsg(String msg) {
        log.info("监听到课程发布消息，课程id：{}", msg);
        //从mqMessage拿到课程id
        long courseId = Long.parseLong(msg);

        //课程静态化上传到minio
        generateCourseHtml(courseId);

        //向elasticsearch写索引数据
        saveCourseIndex(courseId);
    }

    /**
     * 生成课程静态化页面
     *
     * @param courseId 课程id
     */
    private void generateCourseHtml(long courseId) {
        //TODO 开始进行课程静态化 生成html页面

        //TODO 将html上传到minio
    }

    /**
     * 保存课程索引信息
     *
     * @param courseId 课程id
     */
    private void saveCourseIndex(long courseId) {
        //查询课程信息，调用搜索服务添加索引接口
        //从课程发布表查询课程信息
        CoursePublish coursePublish = coursePublishService.getCoursePublish(courseId);

        CourseIndex courseIndex = new CourseIndex();
        //TODO 远程调用搜索微服务，保存课程索引
    }
}
